package com.cn.qgx428.orderingmeal.viewHolder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by QinX on 2017/5/8.
 */

public class RViewHolder extends RecyclerView.ViewHolder {

    private Context mContext;
    private View mConvertView;
    private SparseArray<View> mViews;  //用于作为装载地图的容器

    public RViewHolder(View itemView) {
        super(itemView);
        mConvertView = itemView;
        this.mViews = new SparseArray<View>();
    }

    /**
     * @param context
     * @param parent
     * @param layoutID
     * @param position
     * @return
     * 获取的每一个ViewHolder
     */
    public static RViewHolder get(Context context , ViewGroup parent, int layoutID , int position){
        View view = LayoutInflater.from(context).inflate(layoutID,parent,false);
        return new RViewHolder(view);
    }

    /**
     * 通过控件的ID获取相对的控件，如果没有则加入到mViews
     * @param viewID
     * @param <T>
     * @return
     */
   public <T extends View> T getView(int viewID){
       View view = mViews.get(viewID);
       if (view == null){
           view = mConvertView.findViewById(viewID);
           mViews.put(viewID,view);
       }
       return (T) view;
   }

   public RecyclerView.ViewHolder setText (int viewID, String text){
       TextView view = getView(viewID);
       view.setText(viewID);

       return this;
   }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public RecyclerView.ViewHolder setImageResource(int viewId, int drawableId) {
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }

    public View getConvertView() {
        return mConvertView;
    }

}
